<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'category';
    protected $primaryKey = 'cate_id';
    public $timestamps = false;
    protected $guarded = [];
    public function tree()
    {
        $categorys = $this->orderBy('cate_order','asc')->get();
//        dd($categorys);
        return $this->getTree($categorys,'cate_name','cate_id','cate_pid',0);
    }
    /**
     * @param $data
     * @param $field_name 分类名称
     * @param $field_id  主键ID
     * @param $field_pid    父ID
     * @param string $pid   父ID的值
     * @return array    二级分类数组
     */
    public function getTree($data,$field_name,$field_id,$field_pid,$pid='0'){
        $arr = array();
        foreach($data as $k=>$v){
            if($v->$field_pid==$pid){
                $data[$k]['_'.$field_name] = $data[$k][$field_name];
                $arr[]=$data[$k];
                foreach($data as $m=>$n){
                    if($n->$field_pid==$v->$field_id){
                        $data[$m]['_'.$field_name]='├─'.$data[$m][$field_name];
//                        dd($data[$m]);
                        $arr[]=$data[$m];
                    }
                }
            }
        }
        return $arr;
    }
}
